#!/usr/bin/env python
# -*-coding:UTF-8-*-

products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]


def list_prds(prds, name):
    i = 1
    print("-----  %s列表  -----" % name)
    for item in prds:
        print("%d. %-6s :" % (i, item[0]), end='')
        print("%10s" % item[1])
        i += 1


def shopcar(prds):
    prds1 = {}
    i = 1
    for item in prds:
        prds1[str(i)] = item
        i += 1
    shop_car = []
    while True:
        pro = input('\n请输入购买商品序号，结束购物请输入q:')
        if pro in [str(x) for x in range(1, len(prds)+1)]:
            if pro in prds1.keys():
                shop_car.append(prds1[pro])
                shop_cost1 = [x[1] for x in shop_car]
                list_prds(shop_car, '购物车')
                print('-'*25)
                print('总金额：%d' % sum(shop_cost1))
                del prds1[pro]
            else:
                print("商品已售出")
        elif pro in ['q', 'Q']:
            shop_cost = [x[1] for x in shop_car]
            list_prds(shop_car, '购物车')
            print('-'*25)
            print('总金额：%d' % sum(shop_cost))
            exit()
        else:
            print("商品不存在")


def main():
    list_prds(products, '商品')
    shopcar(products)


if __name__ == '__main__':
    main()

    # 有几个小问题：
    # 1 商品列表是不是应该可以重复提示，就是在每次购买完后，再提示一次。
    # 2 如果有库存的话，是不是可以再加个库存的限制在里面(后续考虑)